package com.org.web.support.base;

import com.org.platform.enums.HttpStatusEnum;
import com.org.web.handle.user.service.UserService;
import com.org.web.handle.vo.UserVo;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;

/**
 * 这个类可以做好多事
 */
@Controller
public class BaseController {

  @Autowired
  private HttpServletRequest request;

  @Autowired
  private UserService userService;

  @ModelAttribute
  public void init() {
  }

  public ModelAndView modelAndView(String viewName) {
    return modelAndView(viewName,null);
  }

  public ModelAndView modelAndView(String viewName, Map<String, Object> model) {
    if (null == model) {
      model = new HashMap<>();
    }
    UserVo userVo = userService.getUser();
    model.put("userVo",userVo);
    model.put("isLogin",userVo == null ? "0" : "1");
    model.put("message",userVo == null ? HttpStatusEnum.ERROR401.getMessage() : "");
    return new ModelAndView(viewName, model);
  }
}
